﻿#pragma once
#include<iostream>
#include<assert.h>
#include<cstdbool>
#include<vector>
using namespace std;
// 枚举值表示颜色
enum Colour
{
	RED,
	BLACK
};
// 这里我们默认按key结构实现 
template<class T>
struct RBTreeNode
{
	// 这里更新控制平衡也要加入parent指针 
	T _data;
	RBTreeNode<T>* _left;
	RBTreeNode<T>* _right;
	RBTreeNode<T>* _parent;
	Colour _col;
	RBTreeNode(const T& data)
		:_data(data)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _col(RED)
	{}
};

// 请模拟实现红黑树的插入--注意：为了后序封装map和set，本文在实现时给红黑树多增加了一个头结点
template<class T>
class RBTree
{
	typedef RBTreeNode<T> Node;
public:
	RBTree()// 增加哨兵位头结点
	{
		_pHead = new Node(0);
		_pHead->_left = _pHead;
		_pHead->_right = _pHead;
	}

	void InOrder()
	{
		_InOrder(GetRoot());
		cout << endl;
	}

	// 在红黑树中插入值为data的节点，插入成功返回true，否则返回false
	// 注意：为了简单起见，本次实现红黑树不存储重复性元素
	bool Insert(const T& data)
	{
		if (GetRoot() == nullptr)// 空树---插入即为根
		{
			Node* root = new Node(data);
			root->_parent = _pHead;
			_pHead->_parent = root;
			_pHead->_left = _pHead->_right = root;
			root->_col = BLACK;
			return true;
		}
		Node* cur = GetRoot();
		Node* parent = nullptr;
		while (cur)
		{
			if (data < cur->_data)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (data > cur->_data)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
				return false;
		}
		cur = new Node(data);
		cur->_parent = parent;
		if (data < parent->_data)
			parent->_left = cur;
		else
			parent->_right = cur;
		// 插入完成，开始维护红黑树的规则
		while (parent != _pHead && parent->_col != BLACK)// 到哨兵位或父亲为黑就可退出
		{
			Node* grandfather = parent->_parent;
			Node* uncle = grandfather->_left;// 假设法先假设
			if (uncle == parent)
				uncle = grandfather->_right;
			// 三种情况---都是主要看uncle
			// 1.只变色---适用于uncle存在且为红(无论cur插入在左还是右皆可如此)
			if (uncle && uncle->_col == RED)
			{
				parent->_col = uncle->_col = BLACK;
				grandfather->_col = RED;
			}
			// 旋转+变色---适用于uncle不存在(即cur必为新增) 或 uncle存在且为黑(即cur必不是新增)---要分单旋和双旋
			else if (uncle == nullptr || uncle && uncle->_col == BLACK)
			{
				// 2.单旋+变色
				// 右单旋
				if (grandfather->_left == parent && parent->_left == cur)// 纯粹左边高
				{
					RotateR(grandfather);
					parent->_col = BLACK;
					grandfather->_col = RED;
				}
				// 左单旋
				else if (grandfather->_right == parent && parent->_right == cur)// 纯粹右边高
				{
					RotateL(grandfather);
					parent->_col = BLACK;
					grandfather->_col = RED;
				}
				// 3.双旋+变色
				// 左右双旋
				else if (grandfather->_left == parent && parent->_right == cur)// 不纯粹右边高
				{
					RotateL(parent);
					RotateR(grandfather);
					cur->_col = BLACK;
					grandfather->_col = RED;
				}
				// 右左双旋
				else if (grandfather->_right == parent && parent->_left == cur)// 不纯粹左边高
				{
					RotateR(parent);
					RotateL(grandfather);
					cur->_col = BLACK;
					grandfather->_col = RED;
				}
				else
					assert(false);
				// 因为有哨兵位的原因，每插入一个结点就要去维护哨兵位结点指针的指向
				// 不能立即插入后就改变指向，因为插入后可能引发旋转，应在旋转后维护
				_pHead->_left = LeftMost();
				_pHead->_right = RightMost();
				break;
			}
			else
				assert(false);
			cur = grandfather;
			parent = cur->_parent;
		}
		// 颜色变化可能导致根变色，这里直接强制控制为黑色
		GetRoot()->_col = BLACK;
		return true;
	}

	// 检测红黑树中是否存在值为data的节点，存在返回该节点的地址，否则返回nullptr
	Node* Find(const T& data)
	{
		if (GetRoot() == nullptr)
			return nullptr;
		Node* cur = GetRoot();
		while (cur)
		{
			if (data < cur->_data)
				cur = cur->_left;
			else if (data > cur->_data)
				cur = cur->_right;
			else
				return cur;
		}
		return nullptr;
	}
	// 获取红黑树最左侧节点
	Node* LeftMost()
	{
		Node* root = GetRoot();
		if (nullptr == root)
			return _pHead;
		Node* cur = root;
		while (cur->_left)
			cur = cur->_left;
		return cur;
	}
	// 获取红黑树最右侧节点
	Node* RightMost()
	{
		Node* root = GetRoot();
		if (nullptr == root)
			return _pHead;
		Node* cur = root;
		while (cur->_right)
			cur = cur->_right;
		return cur;
	}
	// 检测红黑树是否为有效的红黑树，注意：其内部主要依靠_IsValidRBTRee函数检测
	bool IsValidRBTRee()
	{
		if (GetRoot() == nullptr)
			return true;
		size_t pathBlack = 0;
		Node* cur = GetRoot();
		while (cur)// 统计一条路径的黑色结点数作参考
		{
			if (cur->_col == BLACK)
				++pathBlack;
			cur = cur->_left;
		}
		return _IsValidRBTRee(GetRoot(), 0, pathBlack);
	}
	// 求高度
	int Height()
	{
		return _Height(GetRoot());
	}
	// 求数的结点
	int Size()
	{
		return _Size(GetRoot());
	}
private:
	int _Height(Node* pRoot)
	{
		if (pRoot == nullptr)
			return 0;
		int left_Height = _Height(pRoot->_left) + 1;
		int right_Height = _Height(pRoot->_right) + 1;
		return left_Height > right_Height ? left_Height : right_Height;
	}
	int _Size(Node* pRoot)
	{
		if (pRoot == nullptr)
			return 0;
		return _Size(pRoot->_left) + _Size(pRoot->_right) + 1;
	}
	void _InOrder(Node* pRoot)
	{
		if (pRoot == nullptr)
			return;
		_InOrder(pRoot->_left);
		cout << pRoot->_data << " ";
		_InOrder(pRoot->_right);
	}
	bool _IsValidRBTRee(Node* pRoot, size_t blackCount, const size_t pathBlack)// 利用递归栈帧的特性，有多少栈帧就有多少路径，也就需统计多少次blackCount
	{
		if (pRoot == nullptr)
		{
			// 前序遍历，走到空就是一条路径已经走完
			if (blackCount != pathBlack)
			{
				cout << "路径中存在不相同的黑色结点数" << endl;
				return false;
			}
			return true;
		}
		if (pRoot->_col == RED && pRoot->_parent->_col == RED)
		{
			cout << "路径中存在连续的红色结点" << endl;
			return false;
		}
		if (pRoot->_col == BLACK)
			++blackCount;
		return _IsValidRBTRee(pRoot->_left, blackCount, pathBlack) &&
			_IsValidRBTRee(pRoot->_right, blackCount, pathBlack);
	}
	// 左单旋
	void RotateL(Node* pParent)
	{
		Node* subR = pParent->_right;
		Node* subRL = subR->_left;
		Node* grandfather = pParent->_parent;
		pParent->_right = subRL;
		if (subRL)
			subRL->_parent = pParent;
		subR->_left = pParent;
		pParent->_parent = subR;
		if (pParent == GetRoot())
			GetRoot() = subR;
		else
		{
			if (grandfather->_left == pParent)
				grandfather->_left = subR;
			else
				grandfather->_right = subR;
		}
		subR->_parent = grandfather;
	}
	// 右单旋
	void RotateR(Node* pParent)
	{
		Node* subL = pParent->_left;
		Node* subLR = subL->_right;
		Node* grandfather = pParent->_parent;
		pParent->_left = subLR;
		if (subLR)
			subLR->_parent = pParent;
		subL->_right = pParent;
		pParent->_parent = subL;
		if (pParent == GetRoot())// 小心，如果是整个的根，则根结点要变化
			GetRoot() = subL;
		else// 局部根
		{
			if (grandfather->_left == pParent)
				grandfather->_left = subL;
			else
				grandfather->_right = subL;
		}
		subL->_parent = grandfather;
	}
	// 为了操作树简单起见：获取根节点
	Node*& GetRoot()
	{
		return _pHead->_parent;// 没有数据则返回空，因为_pHead->_parent缺省值为nullptr
	}
private:
	Node* _pHead;
};